<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="ppt模板名称" prop="pptName">
        <el-input
          v-model="queryParams.pptName"
          placeholder="请输入ppt模板名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mall:PPtTemplate:add']"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list">
      <el-table-column label="模板名称" align="center" prop="pptName" />
      <el-table-column label="所属分类" align="center" prop="pptCateName" />
      <el-table-column label="头图" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 50px; height: 50px"
            :src="scope.row.pptTopImg"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column label="模板展示图" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 50px; height: 50px"
            :src="scope.row.pptTempImg"
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column label="尾图" align="center">
        <template slot-scope="scope">
          <el-image
            style="width: 50px; height: 50px"
            :src="scope.row.pptBottomImg"
          ></el-image>
        </template>
      </el-table-column>

      <el-table-column label="是否展示" prop="isStatus">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isStatus"
            :active-value="1"
            :inactive-value="0"
            @change="handleShowUser(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mall:PPtTemplate:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mall:PPtTemplate:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改菜单对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="680px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="模板名称" prop="pptName">
              <el-input v-model="form.pptName" placeholder="请输入模板名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="所属分类" prop="cateId">
              <treeselect
                v-model="form.cateId"
                :options="cateOptions"
                :normalizer="normalizer"
                :show-count="true"
                placeholder="选择ppt分类"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="ppt头图" prop="pptTopImg">
              <Image-Upload
                :value="form.pptTopImg"
                :limit="1"
                @input="getTopImgUrl"
              ></Image-Upload>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="ppt尾图" prop="pptBottomImg">
              <Image-Upload
                :value="form.pptBottomImg"
                :limit="1"
                @input="getBottomImgUrl"
              ></Image-Upload>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="模版展示图" prop="pptTempImg">
              <Image-Upload
                :value="form.pptTempImg"
                :limit="1"
                @input="getTempImgUrl"
              ></Image-Upload>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="背景图" prop="pptBgImg">
              <Image-Upload
                :value="form.pptBgImg"
                :limit="1"
                @input="getPptBgImg"
              ></Image-Upload>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="是否展示" prop="isStatus">
              <el-radio-group v-model="form.isStatus">
                <el-radio :label="1">是</el-radio>
                <el-radio :label="0">否</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  pptTemplateList,
  addPPtTemplate,
  queryPPtTemplate,
  updatePPtTemplate,
  delPPtTemplate,
  handleShow,
} from "@/api/ppt/pptComponent";
import { pptCategoryList } from "@/api/ppt/pptCategory";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import IconSelect from "@/components/IconSelect";

export default {
  name: "PPTCategory",
  components: { Treeselect, IconSelect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 菜单表格树数据
      list: [],
      // 菜单树选项
      cateOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部折叠
      isExpandAll: false,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        pptName: [
          { required: true, message: "模板名称不能为空", trigger: "blur" },
        ],
        cateId: [
          { required: true, message: "模板分类不能为空", trigger: "change" },
        ],
      },
      total: 0,
    };
  },
  created() {
    this.getList();
    this.getTreeselect();
  },
  methods: {
    /** 查询菜单列表 */
    getList() {
      this.loading = true;
      pptTemplateList(this.queryParams).then((response) => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.pptCateName,
        children: node.children,
      };
    },
    /** 查询菜单下拉树结构 */
    getTreeselect() {
      pptCategoryList().then((response) => {
        this.cateOptions = this.handleTree(response.data, "id", "pid");
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    getTopImgUrl(e) {
      console.log(e);
      this.form.pptTopImg = e;
    },
    getTempImgUrl(e) {
      this.form.pptTempImg = e;
    },
    getPptBgImg(e) {
      this.form.pptBgImg = e;
    },
    getBottomImgUrl(e) {
      this.form.pptBottomImg = e;
    },
    // 表单重置
    reset() {
      this.form = {
        isStatus: 1,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    //是否展示开关
    handleShowUser(row) {
      const { pptId, isStatus } = row;

      let text = row.isStatus == 1 ? "开启" : "关闭";
      this.$modal
        .confirm("确认要" + text + "展示吗？")
        .then(function () {
          return handleShow({ pptId, isStatus });
        })
        .then(() => {
          this.$modal.msgSuccess(text + "成功");
        })
        .catch(function () {
          row.isStatus = row.isStatus == 1 ? 0 : 1;
        });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加模板";
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      queryPPtTemplate(row.pptId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改模板";
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.pptId != undefined) {
            updatePPtTemplate(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPPtTemplate(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除名称为"' + row.pptName + '"的数据项？')
        .then(function () {
          return delPPtTemplate(row.pptId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
